package com.lazason.wib4android;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.Log;

public class MyApplication extends Application {
	private List<Activity> activityList;
	public static Map<String,ImageInfo> imageInfos=new HashMap<String,ImageInfo>();
	public static BlockingQueue queue;
	public static ThreadPoolExecutor executor;
	public static List<Map<String, Object>> data;
	public static List<Map<String, Object>> friend;
	public static List<Map<String, Object>> mention;
	public static List<Map<String, Object>> comments;
	public static Map<String, Drawable> drawableCache = new HashMap<String, Drawable>();;
	public static HashMap<String, WeakReference<BitmapDrawable>> imageCache = new HashMap<String, WeakReference<BitmapDrawable>>();
	public static HashMap<String, WeakReference<Bitmap>> bitmapCache = new HashMap<String, WeakReference<Bitmap>>();
	public static Drawable icon;
	private static MyApplication instance;
	public static String saveMode = "save";
	private String appPath;
	public static int textColor;
	public static int alpha;
	public static boolean authFlag = false;
	public static boolean isWifi = false;

	public static long holderTime = 0;
	public static long picUrlTime = 0;
	public static long pictureIdTime = 0;
	public static long pictureTime = 0;
	public static long getCachedImageTime = 0;
	public static long setCachedImageTime = 0;
	public static long setProfileTime = 0;
	public static long getWeiboTime = 0;
	public static long setWeiboTime = 0;
	public static long usernameTime = 0;
	public static long WEB_URLSTime = 0;
	public static long userPatternTime = 0;
	public static long lastCheckWifi = 0;
	public static String style = "normal";

	private MyApplication() {
	}

	// 单例模式中获取唯一的MyApplication实例
	public static MyApplication getInstance() {

		if (null == instance) {
			instance = new MyApplication();
			instance.activityList = new LinkedList<Activity>();

		}
		return instance;

	}

	public static boolean isWifi(Context mContext) {
		if (System.currentTimeMillis() - lastCheckWifi > 300000) {
			ConnectivityManager connectivityManager = (ConnectivityManager) mContext
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo activeNetInfo = connectivityManager
					.getActiveNetworkInfo();
			if (activeNetInfo != null
					&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
				isWifi = true;
			} else {
				isWifi = false;
			}
		}
		return isWifi;

	}

	// 添加Activity到容器中
	public void addActivity(Activity activity) {
		activityList.add(activity);
	}

	public void removeActivity(Activity activity) {
		activityList.remove(activity);
	}

	// 遍历所有Activity并finish

	public void exit() {
		finish();

		System.exit(0);

	}

	public void finish() {

		long totalTime = holderTime + picUrlTime + pictureIdTime + pictureTime
				+ getCachedImageTime + setCachedImageTime + setProfileTime
				+ getWeiboTime + setWeiboTime + usernameTime + WEB_URLSTime
				+ userPatternTime;

		Log.d("MyApplication", "total time =" + totalTime);
		Log.d("MyApplication", "holderTime =" + holderTime);
		Log.d("MyApplication", "picUrlTime =" + picUrlTime);
		Log.d("MyApplication", "pictureIdTime =" + pictureIdTime);
		Log.d("MyApplication", "pictureTime =" + pictureTime);
		Log.d("MyApplication", "getCachedImageTime =" + getCachedImageTime);
		Log.d("MyApplication", "setCachedImageTime =" + setCachedImageTime);
		Log.d("MyApplication", "setProfileTime =" + setProfileTime);
		Log.d("MyApplication", "getWeiboTime =" + getWeiboTime);
		Log.d("MyApplication", "setWeiboTime =" + setWeiboTime);
		Log.d("MyApplication", "usernameTime =" + usernameTime);
		Log.d("MyApplication", "WEB_URLSTime =" + WEB_URLSTime);
		Log.d("MyApplication", "userPatternTime =" + userPatternTime);

		for (Activity activity : activityList) {
			// removeActivity(activity);
			activity.finish();

		}
	}

	public static String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		}
		if (sdDir == null) {
			return null;
		}
		return sdDir.getPath();

	}

	public String getAppPath() {
		if (appPath == null) {
			appPath = getSDPath() + "/" + "lazason";
		}
		return appPath;

	}

	public static ThreadPoolExecutor getExecutor() {
		if (queue == null) {
			queue = new LinkedBlockingQueue();
		}
		if (executor == null) {
			executor = new ThreadPoolExecutor(2, 2, 180, TimeUnit.SECONDS,
					queue);
		}
		return executor;
	}

	public static void setExecutor(ThreadPoolExecutor executor) {
		MyApplication.executor = executor;
	}

	public void onLowMemory() {
		for (Activity activity : activityList) {
			// removeActivity(activity);
			if (!(activity instanceof TimelineActivity)) {
				activity.finish();
			}

		}
	}
}
